package com.srs.backend.serviceImpl;

import com.srs.backend.mapper.TableMapper;
import com.srs.backend.pojo.Tables;
import com.srs.backend.service.TableService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service
public class TableServicelmpl implements TableService {
    @Resource
    private TableMapper tableMapper;

    @Override
    public Result add(Map<String, String> data) {
        String maxnumStr = data.get("max_num");
        Integer maxnum = null;
        String remark = data.get("remark");
        String statusStr = data.get("status");
        Integer status = null;

        try {
            maxnum = Integer.parseInt(maxnumStr);
            status = Integer.parseInt(statusStr);
        } catch (NumberFormatException e) {
            return Result.error("数据类型错误");
        }

        tableMapper.insert(new Tables(null, status, maxnum,remark));

        return Result.ok("success");
    }

    @Override
    public Result delete(Map<String, String> data) {
        String tableIdStr = data.get("table_id");
        Integer tableId = null;
        try {
            tableId = Integer.parseInt(tableIdStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        if (tableId == null) {
            return Result.error("当前餐桌不存在或已被删除”");
        }
        tableMapper.deleteById(tableId);
        return Result.ok("success");
    }

    @Override
    public Result modify(Map<String, String> data) {
        String tableIdStr = data.get("id");
        Integer id;
        try {
            id = Integer.parseInt(tableIdStr);
        } catch (NumberFormatException e) {
            return Result.error("餐桌id数据有误");
        }
        Tables curTable = tableMapper.selectById(id);
        if (curTable == null) {
            return Result.error("当前操作餐桌不存在或已被删除");
        }

        String maxNumStr = data.get("max_num");
        Integer maxNum;
        String remark = data.get("remark");
        String statusStr = data.get("status");
        Integer status;

        try {
            maxNum = Integer.parseInt(maxNumStr);
            status = Integer.parseInt(statusStr);
        } catch (NumberFormatException e) {
            return Result.error("数据类型错误");
        }

        curTable.setId(id);
        curTable.setStatus(status);
        curTable.setMaxNum(maxNum);
        curTable.setRemark(remark);

        tableMapper.updateById(curTable);
        return Result.ok("success");
    }

    @Override
    public Result getAll() {
        return Result.ok(tableMapper.selectList(null));
    }
}
